package com.icard.wx.icardwx.service.impl;

import com.dsbj.common.model.Dept;
import com.dsbj.common.model.Employee;
import com.dsbj.common.msg.ResponseMessage;
import com.icard.wx.icardwx.service.IDeptService;
import com.icard.wx.icardwx.service.IEmployeeService;
import com.icard.wx.icardwx.utils.PropertyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeService implements IEmployeeService {

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public ResponseMessage<List<Employee>> searchEmployeeByKeyword(String keywords, String companyno) {
        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.getGatewayHost()).append("/api/icard/employee/getEmployeeByNameDim");

        ParameterizedTypeReference<ResponseMessage<List<Employee>>> typeRef = new ParameterizedTypeReference<ResponseMessage<List<Employee>>>() {
        };
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        Map<String, String> params= new HashMap<>();
        //  也支持中文
        params.put("employeename", keywords);
        params.put("companyno", companyno);
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<Map<String, String>>(params, headers);
        ResponseMessage msg =  restTemplate.exchange(sb.toString(), HttpMethod.POST,requestEntity,typeRef).getBody();
        return msg;
    }
}
